﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using DevComponents.WPF.Controls;
using Microsoft.Practices.Prism.Commands;
using ZhiHeng.Consume.DataContract;
using ZhiHeng.OneCard.CommUtility;

namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
   public class SystemConfigurationViewModel: ViewModelBase
    {
       public SystemConfigurationViewModel()
       {
           Query();
       }
       #region 属性
       private ObservableCollection<Systems> _sysList;
       /// <summary>
       /// SysList
       /// </summary>
       public ObservableCollection<Systems> SysList
       {
           get
           {
               return _sysList;
           }
           set
           {
               _sysList = value;
               this.RaisePropertyChanged("SysList");
           }
       }
       private Systems _sysData;
       /// <summary>
       /// SysData
       /// </summary>
       public Systems SysData
       {
           get
           {
               return _sysData;
           }
           set
           {
               _sysData = value;
               this.RaisePropertyChanged("SysData");
           }
       }
       #endregion
        #region 逻辑
       void Query()
       {
           SysList = new ObservableCollection<Systems>();
           new systemConfiguration().ExtGetAll()
               .ForEach(x => SysList.Add(new Systems() 
               { code=x.code, Flag=x.Flag, name=x.name, Remark=x.Remark, value=x.value }));
       }
       /// <summary>
       /// 利用反射赋值
       /// </summary>
       /// <param name="source">源数据</param>
       /// <param name="target">目标数据，不能为Null</param>
       public void RefGetValue(object source, object target)
       {
           if (source != null)
           {

               Type t = source.GetType();
               PropertyInfo[] pi = t.GetProperties();
               foreach (PropertyInfo p in pi)
               {
                   try
                   {
                       PropertyInfo pii = source.GetProperty(p.Name);
                       pii.SetValue(target, p.GetValue(source, null), null);
                   }
                   catch
                   {
                   }
               }
           }
       }
        #endregion
        #region 事件
       private DelegateCommand _updateCommand;
       /// <summary>
       /// UpdateCommand
       /// </summary>
       public DelegateCommand UpdateCommand
       {
           get
           {
               return _updateCommand ?? (_updateCommand = new DelegateCommand(() =>
               {
                   try
                   {
                       var Dsp = new account().ExtGetSingle("it.code == @code ", false, new SearchParameter("code", SysData.code));
                       if (Dsp == null) return;
                       RefGetValue(SysData, Dsp);
                       Dsp.ExtUpdate();
                       ShowMsg("修改成功！");
                      
                   }
                   catch (Exception ex)
                   {
                       ShowMsg("更新失败!Error："+ex.Message.ToString(),false);
                   }
                   finally
                   {
                       Query();
                   }
               }));
           }
       }

       public DelegateCommand OkCommand
       {
           get { return MainViewModel.Instance.ReturnToStartCommand; }
       }
        #endregion
    }
   public class Systems : ZhiHeng.Consume.DataContract.systemConfiguration
   {
   }
}
